// components/timePicker/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    isShowpopup: {
        type: Boolean,
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    currentDate: new Date().getTime(),
    // minDate: new Date(1940, 0, 1).getTime(),
    minDate: new Date(new Date().getFullYear()-1, new Date().getMonth(), 1).getTime(),
    maxDate: new Date().getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 打开日期
    showPop(data) {
      this.setData({
          isShowpopup: data
      })
    },
    // 选择日期
    onInput(event) {
      this.setData({
          currentDate: event.detail,
      });
    },
    // 选择日期 确认按钮
    onConfirm(val) {
      let a = this._timeFormat(val.detail);
      let arr = a.split('-')
      // console.log(arr)
      this.triggerEvent('showing',{
          selectDate: arr,
          isShowpopup: false
      })
    },
    // 取消按钮
    onCancel(){
      this.triggerEvent('canceling',{
          isShowpopup: false
      })
    },
    _timeFormat(time) { // 时间格式化 2019-09-08
      var time = new Date(time);
      let year = time.getFullYear();
      let month = time.getMonth() + 1;
      let day = time.getDate();
      let h = time.getHours(); h = h < 10 ? ('0' + h) : h;
      let minutes = time.getMinutes(); minutes = minutes < 10 ? ('0' + minutes) : minutes;		
      return year + '-' + month
    }
  }
})
